<template>
  <!-- 选择套餐规格弹窗 -->
  <Dialog
    ref="skuFormDialogRef"
    :title="title"
    v-model="formVisible"
    width="50%"
    class="enhanced-dialog"
  >
    <!-- 弹窗头部 -->
    <div class="dialog-header">
      <div class="header-icon">
        <Icon icon="ep:shopping-bag" />
      </div>
      <div class="header-content">
        <h4 class="dialog-title">{{ title || '续费套餐' }}</h4>
        <p class="dialog-subtitle">选择合适的续费套餐</p>
      </div>
    </div>

    <!-- 套餐选择区域 -->
    <div class="package-container">
      <div class="package-grid">
        <div
          v-for="(item, index) in itemList"
          :key="index + 'renew'"
          @click="selectItem(item)"
          class="package-item"
          :class="{ active: orderConfirmParams.itemId == item.id }"
        >
          <div class="package-content">
            <div class="package-duration">
              <Icon icon="ep:calendar" class="duration-icon" />
              <span class="duration-text">{{ item.value }}天</span>
            </div>

            <div class="package-divider"></div>

            <div class="package-price">
              <Icon icon="ep:coin" class="price-icon" />
              <span class="price-label">单价：</span>
              <span class="price-value">{{ item.price }}元</span>
            </div>

            <div class="package-badge" v-if="orderConfirmParams.itemId == item.id">
              <Icon icon="ep:check" />
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 订单信息 -->
    <div class="order-summary">
      <div class="summary-item">
        <Icon icon="ep:wallet" class="summary-icon" />
        <span class="summary-label">订单金额：</span>
        <span class="summary-value">￥{{ orderAmount }}</span>
      </div>
    </div>

    <!-- 弹窗底部 -->
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="formVisible = false" class="cancel-btn">
          <Icon icon="ep:close" class="btn-icon" />
          取消
        </el-button>
        <el-button
          :loading="confirmLoading"
          type="primary"
          @click="onConfirmOrder"
          :disabled="!orderConfirmParams.itemId"
          class="confirm-btn"
        >
          <Icon icon="ep:check" class="btn-icon" v-if="!confirmLoading" />
          确认订单
        </el-button>
      </div>
    </template>
  </Dialog>
</template>

<script setup>
import * as PayApi from '@/api/system/pay'
import { getProductRenewByCode } from '@/api/system/productRenew'

defineOptions({ name: 'RenewPackageDialog' })

// 选中的sku对象
const selectedItem = ref(null)

/** 弹窗显隐 */
const formVisible = ref(false)
/** 规格套餐list */
const itemList = ref([])

/** 提交上传form */
const orderConfirmParams = reactive({
  bizType: '',
  bizId: null,
  itemId: null,
  num: 1
})
/** 按钮加载动作 */
const confirmLoading = ref(false)
/** 弹窗的ref */
const skuFormDialogRef = ref()
/*弹窗标题*/
const title = ref('')
// 订单金额
const orderAmount = computed(() => {
  if (selectedItem.value) {
    return ((selectedItem.value.price * 100) / 100) * orderConfirmParams.num
  } else {
    return 0
  }
})

/** 打开弹窗 */
const open = async (productCode, bizId, num) => {
  if (!bizId) {
    return
  }
  if (num) {
    orderConfirmParams.num = num
  }
  formVisible.value = true
  itemList.value = []
  if (productCode) {
    itemList.value = await getProductRenewByCode(productCode)
  }
  orderConfirmParams.bizId = bizId
  orderConfirmParams.bizType = productCode
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

const orderType = ref(null)

// 选择规格
const selectItem = (sku) => {
  orderConfirmParams.itemId = sku.id
  selectedItem.value = sku
}

const { push } = useRouter()

// 确认订单
const onConfirmOrder = async () => {
  confirmLoading.value = true
  ElMessage.success('订单正在创建')
  const data = await PayApi.confirmRenewalOrder({ ...orderConfirmParams })
  confirmLoading.value = false
  push('/pay/balance?preOrderNo=' + data.preOrderNo)
}
</script>

<style lang="scss" scoped>
// 主题色变量
$primary-color: #0052cc;
$primary-light: rgba(0, 82, 204, 0.1);
$primary-border: rgba(0, 82, 204, 0.3);
$success-color: #67c23a;
$warning-color: #e6a23c;
$danger-color: #f56c6c;
$info-color: #909399;
$text-primary: #303133;
$text-regular: #606266;
$text-secondary: #909399;
$text-placeholder: #c0c4cc;
$border-color: #dcdfe6;
$border-light: #e4e7ed;
$bg-color: #f5f7fa;
$shadow-light: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
$shadow-base:
  0 2px 4px rgba(0, 0, 0, 0.12),
  0 0 6px rgba(0, 0, 0, 0.04);
$border-radius: 12px;
$border-radius-small: 8px;

/* 弹窗样式 */
.enhanced-dialog {
  :deep(.el-dialog) {
    border-radius: $border-radius;
    overflow: hidden;

    @media (max-width: 768px) {
      width: 95% !important;
      margin: 0 auto;
    }
  }

  :deep(.el-dialog__header) {
    padding: 0;
    border-bottom: none;
  }

  :deep(.el-dialog__body) {
    padding: 0;
  }

  :deep(.el-dialog__footer) {
    padding: 0;
    border-top: none;
  }

  :deep(.el-dialog__headerbtn) {
    top: 16px;
    right: 16px;
    z-index: 10;

    .el-dialog__close {
      color: white;
      font-size: 18px;

      &:hover {
        color: rgba(255, 255, 255, 0.8);
      }
    }
  }
}

/* 弹窗头部 */
.dialog-header {
  display: flex;
  align-items: center;
  padding: 20px 24px;
  background: linear-gradient(135deg, $primary-color 0%, #1d4ed8 100%);
  color: white;

  @media (max-width: 768px) {
    padding: 16px 20px;
    flex-direction: column;
    gap: 8px;
    text-align: center;
  }
}

.header-icon {
  width: 40px;
  height: 40px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: $border-radius-small;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  flex-shrink: 0;

  @media (max-width: 768px) {
    width: 36px;
    height: 36px;
    font-size: 16px;
  }
}

.header-content {
  flex: 1;
  margin-left: 12px;

  @media (max-width: 768px) {
    margin-left: 0;
  }
}

.dialog-title {
  font-size: 18px;
  font-weight: 600;
  margin: 0 0 4px 0;

  @media (max-width: 768px) {
    font-size: 16px;
  }
}

.dialog-subtitle {
  font-size: 13px;
  opacity: 0.9;
  margin: 0;

  @media (max-width: 768px) {
    font-size: 12px;
  }
}

/* 套餐容器 */
.package-container {
  padding: 24px;
  background: white;
  max-height: 60vh;
  overflow-y: auto;

  @media (max-width: 768px) {
    padding: 16px;
    max-height: 50vh;
  }

  /* 滚动条美化 */
  &::-webkit-scrollbar {
    width: 6px;
  }

  &::-webkit-scrollbar-track {
    background: rgba(0, 0, 0, 0.05);
    border-radius: 3px;
  }

  &::-webkit-scrollbar-thumb {
    background: rgba(0, 82, 204, 0.3);
    border-radius: 3px;
    transition: all 0.3s ease;

    &:hover {
      background: rgba(0, 82, 204, 0.5);
    }
  }
}

.package-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 16px;

  @media (max-width: 768px) {
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    gap: 12px;
  }

  @media (max-width: 480px) {
    grid-template-columns: 1fr;
    gap: 10px;
  }
}

.package-item {
  position: relative;
  cursor: pointer;
  transition: all 0.3s ease;
  border-radius: $border-radius-small;
  overflow: hidden;

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 25px rgba(0, 82, 204, 0.15);
  }

  &.active {
    transform: translateY(-2px);
    box-shadow: 0 8px 25px rgba(0, 82, 204, 0.25);

    .package-content {
      border-color: $primary-color;
      background: linear-gradient(135deg, #f8f9ff 0%, #f0f2ff 100%);
    }

    .duration-text {
      color: $primary-color;
    }

    .price-value {
      color: $primary-color;
    }
  }
}

.package-content {
  background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
  border: 2px solid $border-light;
  border-radius: $border-radius-small;
  padding: 24px 16px;
  text-align: center;
  position: relative;
  transition: all 0.3s ease;

  @media (max-width: 768px) {
    padding: 20px 12px;
  }

  @media (max-width: 480px) {
    padding: 16px 10px;
  }
}

.package-duration {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  margin-bottom: 16px;

  @media (max-width: 768px) {
    margin-bottom: 12px;
  }
}

.duration-icon {
  color: $primary-color;
  font-size: 20px;

  @media (max-width: 768px) {
    font-size: 18px;
  }
}

.duration-text {
  font-size: 24px;
  font-weight: 700;
  color: $text-primary;
  transition: color 0.3s ease;

  @media (max-width: 768px) {
    font-size: 20px;
  }

  @media (max-width: 480px) {
    font-size: 18px;
  }
}

.package-divider {
  height: 1px;
  background: linear-gradient(90deg, transparent 0%, $border-color 50%, transparent 100%);
  margin: 16px 0;

  @media (max-width: 768px) {
    margin: 12px 0;
  }
}

.package-price {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  flex-wrap: wrap;
}

.price-icon {
  color: $warning-color;
  font-size: 16px;
}

.price-label {
  font-size: 14px;
  color: $text-secondary;
  font-weight: 500;

  @media (max-width: 768px) {
    font-size: 13px;
  }
}

.price-value {
  font-size: 16px;
  font-weight: 700;
  color: $danger-color;
  transition: color 0.3s ease;

  @media (max-width: 768px) {
    font-size: 15px;
  }
}

.package-badge {
  position: absolute;
  top: -2px;
  right: -2px;
  width: 24px;
  height: 24px;
  background: $primary-color;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 12px;
  box-shadow: 0 2px 8px rgba(0, 82, 204, 0.3);

  @media (max-width: 768px) {
    width: 20px;
    height: 20px;
    font-size: 10px;
  }
}

/* 订单摘要 */
.order-summary {
  padding: 20px 24px;
  background: linear-gradient(135deg, #f8f9ff 0%, #f0f2ff 100%);
  border-top: 1px solid $border-light;

  @media (max-width: 768px) {
    padding: 16px 20px;
  }
}

.summary-item {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-size: 16px;
  font-weight: 600;

  @media (max-width: 768px) {
    font-size: 15px;
  }
}

.summary-icon {
  color: $primary-color;
  font-size: 18px;

  @media (max-width: 768px) {
    font-size: 16px;
  }
}

.summary-label {
  color: $text-regular;
}

.summary-value {
  color: $primary-color;
  font-size: 18px;
  font-weight: 700;

  @media (max-width: 768px) {
    font-size: 16px;
  }
}

/* 弹窗底部 */
.dialog-footer {
  display: flex;
  justify-content: center;
  gap: 12px;
  padding: 20px 24px;
  background: #fafbff;
  border-top: 1px solid $border-light;

  @media (max-width: 768px) {
    padding: 16px 20px;
    flex-direction: column;
  }
}

.confirm-btn {
  background: linear-gradient(135deg, $primary-color 0%, #1d4ed8 100%);
  border: none;
  border-radius: $border-radius-small;
  padding: 12px 24px;
  font-weight: 500;
  color: white;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 6px;

  &:hover:not(:disabled) {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0, 82, 204, 0.3);
  }

  &:disabled {
    opacity: 0.6;
    cursor: not-allowed;
    transform: none;
  }

  @media (max-width: 768px) {
    width: 100%;
    justify-content: center;
  }

  .btn-icon {
    font-size: 14px;
  }
}

.cancel-btn {
  border: 2px solid $border-color;
  color: $text-regular;
  border-radius: $border-radius-small;
  padding: 12px 24px;
  font-weight: 500;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 6px;

  &:hover {
    border-color: $primary-color;
    color: $primary-color;
    background: $primary-light;
  }

  @media (max-width: 768px) {
    width: 100%;
    justify-content: center;
  }

  .btn-icon {
    font-size: 14px;
  }
}

/* 加载状态优化 */
:deep(.el-loading-mask) {
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(2px);

  .el-loading-spinner {
    .path {
      stroke: $primary-color;
    }
  }
}

/* 深色模式适配 */
@media (prefers-color-scheme: dark) {
  .package-container,
  .order-summary {
    background: #374151;
  }

  .dialog-footer {
    background: #4b5563;
    border-color: #6b7280;
  }

  .dialog-title {
    color: #f9fafb;
  }

  .package-content {
    background: linear-gradient(135deg, #4b5563 0%, #374151 100%);
    border-color: #6b7280;
  }

  .package-item.active .package-content {
    background: linear-gradient(135deg, #1e3a8a 0%, #1e40af 100%);
    border-color: #60a5fa;
  }

  .duration-text,
  .summary-label,
  .price-label {
    color: #f9fafb;
  }

  .package-item.active {
    .duration-text,
    .price-value {
      color: #60a5fa;
    }
  }

  .summary-value {
    color: #60a5fa;
  }

  .package-divider {
    background: linear-gradient(90deg, transparent 0%, #6b7280 50%, transparent 100%);
  }
}

/* 响应式优化 */
@media (max-width: 480px) {
  .dialog-header {
    padding: 12px 16px;
  }

  .package-container {
    padding: 12px;
  }

  .order-summary {
    padding: 12px 16px;
  }

  .dialog-footer {
    padding: 12px 16px;
    gap: 8px;
  }

  .summary-item {
    font-size: 14px;
  }

  .summary-value {
    font-size: 15px;
  }
}
</style>
